package com.launcher.wkang.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.launcher.wkang.App;
import com.launcher.wkang.fragment.Fragment1;
import com.launcher.wkang.fragment.Fragment2;
import com.launcher.wkang.fragment.Fragment3;
import com.launcher.wkang.fragment.Fragment4;
import com.launcher.wkang.fragment.Fragment5;
import com.launcher.wkang.util.PhoneUtil;

import java.util.logging.Handler;

public class LauncherViewPager extends ViewPager {
    private float startX;
    private float endX;
    private int maxNum=5;
    private int screenWidth;
    private int lastNum;
    private int currentNum;
    private int distance=100;
    public LauncherViewPager(Context context) {
        super(context);
        screenWidth=PhoneUtil.getWindowWidth(context);
        lastNum=0;
        currentNum=0;
    }

    public LauncherViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        screenWidth=PhoneUtil.getWindowWidth(context);
        lastNum=0;
        currentNum=0;
    }




    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (App.getINstance().isProhibit){
            return false;
        }else {
            return super.dispatchTouchEvent(ev);
        }
    }
    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        // 先保存手指按下的x轴的坐标
        if (ev.getAction () == MotionEvent.ACTION_DOWN)
            startX = ev.getX ();
        if (ev.getAction () == MotionEvent.ACTION_MOVE) {
            /*
        计算手指移动时的坐标跟按下的坐标之间的绝对值，如果超过给定的值，
        就认为viewpager需要滚动。通过调节distance的大小，可以改变滑动
                灵敏度
              */
            if (Math.abs (ev.getX () - startX) < distance)
                return false;
            else
                // 意思就是：touch事件已经被PeopleViewPager自己消费了，不会传递到子控件
                 return true;
        }
        // 其他情况，依旧保持默认的处理方法
        return super.onInterceptTouchEvent (ev);
    }
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            startX = ev.getRawX();
        }else if(ev.getAction() == MotionEvent.ACTION_UP) {
            endX=ev.getRawX();
           if ((startX>endX)&&((startX-endX)>screenWidth/10)){
               int currentNum=getCurrentItem();
                   currentNum=getCurrentItem();
                   currentNum++;
                   if(currentNum>4){
                       currentNum=0;
                   }
                   setCurrentItem(currentNum);



           }else if ((endX>startX)&&((endX-startX)>screenWidth/10 )){
               int currentNum=getCurrentItem();

                   currentNum=getCurrentItem();
                   currentNum--;
                   if (currentNum<0){
                       currentNum=4;
                   }
                   setCurrentItem(currentNum);

           }
        }
            return true;
    }

}
